package ThirdCommunication;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class UI implements BasicData {
    //登录界面组件
    private JFrame log_Frame;//窗体
    private JTextField log_idField;//账号输入框
    private JTextField log_pwdField;//密码输入框
    //注册界面组件
    private JFrame reg_Frame;
    private JTextField reg_idField;
    private JTextField reg_pwdField;
    //聊天界面组件
    private JFrame chat_Frame;
    private JFrame fileFrame;
    private JTextArea chat_showArea = new JTextArea(chat_showArea_row,chat_showArea_col);//消息显示框
    private JTextArea chat_inputArea;//用户输入框
    private JPanel chat_Panel;//聊天面板
    private JPanel user_Panel;//显示好友面板
    private JList aliveList = new JList();//在线好友列表
    private ClientThread clientthread = new ClientThread("localhost",9090,this,aliveList);
    private Listener listener;
    private JFrame receiveFrame;//有文件传过来时显示提示窗体
    private JFrame fileSaveFrame;//选择文件存储的窗体

    private JFrame FileLoadingFrame;//文件发送中过渡窗体



    public void showLoginUI() throws IOException {
        log_Frame = new JFrame("Login");
        log_Frame.setSize(log_Frame_width,log_Frame_height);
        log_Frame.setDefaultCloseOperation(3);
        log_Frame.setLocationRelativeTo(null);
        log_Frame.setLayout(new FlowLayout());

        JLabel jlb1 = new JLabel("ID");
        log_idField = new JTextField(log_idField_col);
        JLabel jlb2 = new JLabel("PWD");
        log_pwdField = new JTextField(log_pwdField_col);

        listener = new Listener(clientthread,this);
        listener.setLog_idField(log_idField);
        listener.setLog_pwdField(log_pwdField);

        JButton jbu1 = new JButton("Login");
        JButton jbu2 = new JButton("Register");
        jbu1.addActionListener(listener);
        jbu2.addActionListener(listener);

        log_Frame.add(jlb1);
        log_Frame.add(log_idField);
        log_Frame.add(jlb2);
        log_Frame.add(log_pwdField);
        log_Frame.add(jbu1);
        log_Frame.add(jbu2);

        log_Frame.setVisible(true);

        //一打开界面就开启连接
        clientthread.connectServer();
        clientthread.start();
    }

    public void showRegisterUI(){
        reg_Frame = new JFrame("Register");
        reg_Frame.setSize(reg_Frame_width,reg_Frame_height);
        reg_Frame.setDefaultCloseOperation(3);
        reg_Frame.setLocationRelativeTo(null);
        reg_Frame.setLayout(new FlowLayout());

        JLabel jlb1 = new JLabel("ID");
        reg_idField = new JTextField(reg_idField_col);
        JLabel jlb2 = new JLabel("PWD");
        reg_pwdField = new JTextField(reg_pwdField_col);

        listener.setReg_idField(reg_idField);
        listener.setReg_pwdField(reg_pwdField);

        JButton jbu = new JButton("Confirm");
        jbu.addActionListener(listener);

        reg_Frame.add(jlb1);
        reg_Frame.add(reg_idField);
        reg_Frame.add(jlb2);
        reg_Frame.add(reg_pwdField);
        reg_Frame.add(jbu);

        reg_Frame.setVisible(true);
    }
    public void showChatUI(){
        chat_Frame = new JFrame("Chat");
        chat_Frame.setSize(chat_Frame_width,chat_Frame_height);
        chat_Frame.setDefaultCloseOperation(3);
        chat_Frame.setLocationRelativeTo(null);
        chat_Frame.setLayout(new BorderLayout());

        //好友面板
        user_Panel = new JPanel();
        user_Panel.setBackground(Color.gray);
        user_Panel.setPreferredSize(new Dimension(user_Panel_width,user_Panel_height));
        aliveList.setPreferredSize(new Dimension(250,750));
        aliveList.setBackground(Color.gray);
        //aliveList.setFont(new Font("Courier",Font.PLAIN,20));
        aliveList.addListSelectionListener(listener);//添加列表选择监听器
        user_Panel.add(aliveList);
        chat_Frame.add(user_Panel,BorderLayout.WEST);

        //聊天面板
        chat_Panel = new JPanel();
        chat_Panel.setPreferredSize(new Dimension(chat_Panel_width,chat_Panel_height));
        //用户输入部分
        JLabel jlb1 = new JLabel("Input:");
        chat_inputArea = new JTextArea(chat_inputArea_row,chat_inputArea_col);
        JButton jbu1 = new JButton("sendALL");
        jbu1.addActionListener(listener);
        JButton jbu2 = new JButton("fileALL");
        jbu2.addActionListener(listener);
        JButton jbu3 = new JButton("quit");
        jbu3.addActionListener(listener);

        chat_Panel.add(chat_showArea);//消息显示框
        chat_Panel.add(jlb1);//添加输入框标签
        chat_Panel.add(chat_inputArea);//添加输入框
        chat_Panel.add(jbu1);//添加发送按钮
        chat_Panel.add(jbu2);//添加发送文件按钮
        chat_Panel.add(jbu3);//添加退出按钮
        chat_Frame.add(chat_Panel,BorderLayout.CENTER);

        chat_Frame.setVisible(true);
        chat_inputArea.requestFocus();//设置鼠标光标在聊天区
        listener.setChat_ALL_inputArea(chat_inputArea);
        listener.setAliveList(aliveList);
    }

    //展示发送文件窗体
    public void showFileUI(){
        fileFrame = new JFrame("File");
        fileFrame.setSize(450, 300);
        fileFrame.setLocationRelativeTo(null);
        fileFrame.setDefaultCloseOperation(3);
        fileFrame.setLayout(new FlowLayout());

        JLabel jlb = new JLabel("File:");
        fileFrame.add(jlb);
        //文件选择的文本框
        JTextField jtf = new JTextField();
        jtf.setPreferredSize(new Dimension(330,30));
        fileFrame.add(jtf);

        JButton jbu1 = new JButton("chooseFileALL");
        JButton jbu2 = new JButton("sendFileALL");
        jbu1.addActionListener(listener);
        jbu2.addActionListener(listener);
        fileFrame.add(jbu1);
        fileFrame.add(jbu2);
        listener.setFile_ALL_Jtf(jtf);
        fileFrame.setVisible(true);
    }
    public void closeFileUI(){fileFrame.dispose();}
    //退出程序
    public void close(){
        System.exit(0);
    }
    //处理登录结果
    public void loginAction(String result) {
        if (result.equals("Succeed.")) {
            showChatUI();
            listener.setMyID(log_idField.getText());
            log_Frame.dispose();//关闭登录窗口
        } else {//弹出对话框显示失败信息
            JOptionPane.showMessageDialog(log_Frame, result);
        }
    }
    //处理注册结果
    public void registerAction(String result){
        //弹出对话框显示注册结果
        JOptionPane.showMessageDialog(reg_Frame,result);
        reg_Frame.dispose();
    }
    //更新聊天面板消息内容
    public void showMessage(String message){
        chat_showArea.append(message+"\r\n");
    }
    //展示是否接收文件窗体
    public void showFileReceiveUI(String fromID){
        receiveFrame = new JFrame("tip");

        receiveFrame.setName(fromID);//以文件发送方ID作为窗体名

        receiveFrame.setSize(log_Frame_width+50,log_Frame_height-100);
        receiveFrame.setLocationRelativeTo(null);
        receiveFrame.setDefaultCloseOperation(3);
        receiveFrame.setLayout(new FlowLayout());

        JLabel tipLable = new JLabel("You have a file from "+fromID+".");
        receiveFrame.add(tipLable);
        JButton jbu1 = new JButton("receiveFile");
        JButton jbu2 = new JButton("refuseFile");
        jbu1.addActionListener(listener);
        jbu2.addActionListener(listener);
        receiveFrame.add(jbu1);
        receiveFrame.add(jbu2);
        receiveFrame.setVisible(true);
        listener.setReceiveFrame(receiveFrame);
    }

    //展示选择存储文件的路径的窗体

    public void showFileSaveUI(){
        fileSaveFrame = new JFrame("File");
        fileSaveFrame.setSize(450, 300);
        fileSaveFrame.setLocationRelativeTo(null);
        fileSaveFrame.setDefaultCloseOperation(3);
        fileSaveFrame.setLayout(new FlowLayout());

        JLabel jlb = new JLabel("File:");
        fileSaveFrame.add(jlb);
        //文件选择的文本框
        JTextField saveFilejtf = new JTextField();
        saveFilejtf.setPreferredSize(new Dimension(330,30));
        fileSaveFrame.add(saveFilejtf);

        JButton jbu1 = new JButton("chooseSaveFile");
        JButton jbu2 = new JButton("saveFile");
        jbu1.addActionListener(listener);
        jbu2.addActionListener(listener);
        fileSaveFrame.add(jbu1);
        fileSaveFrame.add(jbu2);
        listener.setFileSaveFrame(fileSaveFrame);
        listener.setSaveFilejtf(saveFilejtf);
        fileSaveFrame.setVisible(true);
    }

    //展示文件发送过渡窗体
    public void showFileLoadingUI(int choose){
        FileLoadingFrame = new JFrame("Loading");
        FileLoadingFrame.setSize(150,150);
        FileLoadingFrame.setLocationRelativeTo(null);
        FileLoadingFrame.setDefaultCloseOperation(3);
        FileLoadingFrame.setLayout(new BorderLayout());

        String message;
        if(choose==1){
            message = "File is sending...";
        }else{
            message = "File is saving...";
        }
        JLabel jlb = new JLabel(message,SwingConstants.CENTER);
        FileLoadingFrame.add(jlb,BorderLayout.CENTER);

        FileLoadingFrame.setVisible(true);
    }
    //关闭FileLoadingUI窗体
    public void closeFileLoadingUI(){
        FileLoadingFrame.dispose();
    }

    //选择
    public static void main(String[] args) throws IOException {
        new UI().showLoginUI();
    }
}
